local extension = Package:new("wq_zhanwang")
extension.extensionName = "FkWqmt"
Fk:loadTranslationTable{
  ["wq_zhanwang"] = "无期迷途·谵妄",
}

local Wqc = require "packages/FkWqmt/wq_common"
local U = require "packages/utility/utility"

-- 禁闭者：黛伦
local wq_dailun = General:new(extension, "wq_dailun", "qun", 4, 4, General.Female)
-- 技能：剧本
local wq_skill_juben = fk.CreateTriggerSkill{
  name = "wq_skill_juben",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events ={fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.trueName == "slash" and player:getMark("wq_skill_ganshe_invoked") >= 2
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(data.to)
    local choices = {}
    table.insert(choices, "#wq_skill_juben_draw")
    if not to:isNude() then
      table.insert(choices, "#wq_skill_juben_discard::"..to.id)
    end
    local choice = room:askForChoice(player, choices, self.name)
    if choice:startsWith("#wq_skill_juben_draw") then
      player:drawCards(1, self.name)
    else
      if not to:isNude() then
        local cid = room:askForCardChosen(player, to, "he", self.name)
        if cid then
          room:throwCard(cid, self.name, to, player)
        end
      end
    end
  end,
}
local wq_skill_juben_buff = fk.CreateTargetModSkill{
  name = "#wq_skill_juben_buff",
  frequency = Skill.Compulsory,
  bypass_distances =  function(self, player, skill)
    return skill.trueName == "slash_skill" and player:hasSkill("wq_skill_juben")
  end,
  residue_func = function(self, player, skill, scope, card)
    return skill.trueName == "slash_skill" and player:hasSkill("wq_skill_juben") and player:getMark("wq_skill_ganshe_invoked") >= 1 and 1 or 0
  end,
}
wq_skill_juben:addRelatedSkill(wq_skill_juben_buff)
-- 技能：干涉
local wq_skill_ganshe = fk.CreateTriggerSkill{
  name = "wq_skill_ganshe",
  anim_type = "switch",
  switch_skill_name = "wq_skill_ganshe",
  frequency = Skill.Wake,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and
      ((player:getSwitchSkillState(self.switchSkillName, false) == fk.SwitchYang and player.phase == Player.Start) or
      (player:getSwitchSkillState(self.switchSkillName, false) == fk.SwitchYin and player.phase == Player.Finish)) then
        if player:usedSkillTimes(self.name, Player.HistoryGame) == 0 then
          return true
        end
    end
  end,
  can_wake = function(self, event, target, player, data)
    local room = player.room
    if player:getSwitchSkillState(self.switchSkillName, false) == fk.SwitchYang then
      return room:getTag("RoundCount") >= 2
    else
      return player:usedCardTimes("slash", Player.HistoryGame) >= 3
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getSwitchSkillState(self.switchSkillName, true) == fk.SwitchYang then   -- on_use 的时候已经转换了，所以要用 true
      local num = player.maxHp - player:getHandcardNum()
      if num > 0 then
        room:drawCards(player, num, self.name)
      end
      if player:isAlive() then
        room:addPlayerMark(player, "wq_skill_ganshe_invoked", 1)
        player:setSkillUseHistory(self.name, 0, Player.HistoryGame)
      end
    else
      room:changeMaxHp(player, -1)
      if player:isWounded() then
        room:recover({
          who = player,
          num = 1,
          recoverBy = player,
          skillName = self.name
        })
      end
      if player:isAlive() then
        room:addPlayerMark(player, "wq_skill_ganshe_invoked", 1)
      end
    end
  end,
}
-- 技能：无用
local wq_skill_wuyong = fk.CreateActiveSkill{
  name = "wq_skill_wuyong",
  anim_type = "offensive",
  card_num = 1,
  target_num = 1,
  prompt = function (self, selected, selected_cards)
    local num = math.min(math.max(Self:getMark("wq_skill_ganshe_invoked") + 1, 1), 3)
    local prompts = ""
    local current = Self:getMark("wq_skill_wuyong_index")
    local n = 1
    for i = current, current + num - 1, 1 do
      local order = i % 3 + 1
      prompts = prompts.."<br>"..n.."."..Fk:translate("#wq_skill_wuyong_prompt-order_"..order)
      n = n + 1
    end
    return "#wq_skill_wuyong_prompt:::"..num..":"..prompts
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) < 3 and not player:isKongcheng()
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:currentRoom():getCardArea(to_select) == Player.Hand and (not Self:prohibitDiscard(Fk:getCardById(to_select)))
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local card = effect.cards[1]
    room:throwCard(card, self.name, player, player)
    local num = math.min(math.max(player:getMark("wq_skill_ganshe_invoked") + 1, 1), 3)
    local current = player:getMark("wq_skill_wuyong_index")
    for i = current, current + num - 1, 1 do
      if player:isAlive() and target:isAlive() then
        local n = i % 3 + 1
        if n == 1 then
          if not target:isNude() then
            room:askForDiscard(target, 1, 1, true, self.name, false)
          end
        elseif n == 2 then
          if not target:isKongcheng() then
            local cids = room:askForCard(target, 1, 1, false, self.name, false, ".", "#wq_skill_wuyong_give:"..player.id)
            room:obtainCard(player.id, cids[1], false, fk.ReasonGive)
          end
        elseif n == 3 then
          room:damage{
            from = player,
            to = target,
            damage = 1,
            skillName = self.name,
          }
        end
      end
    end
    if player:isAlive() then
      current = (current + num) % 3
      room:setPlayerMark(player, "wq_skill_wuyong_index", current)
    end
  end,
}
-- 黛伦技能
wq_dailun:addSkill(wq_skill_juben)
wq_dailun:addSkill(wq_skill_ganshe)
wq_dailun:addSkill(wq_skill_wuyong)
-- 黛伦翻译
Fk:loadTranslationTable{
  ["wq_dailun"] = "黛伦",
  ["#wq_dailun"] = "虚景妄想",
  ["designer:wq_dailun"] = "妙霄",
  ["cv:wq_dailun"] = "官方",
  ["illustrator:wq_dailun"] = "官方",
  ["wq_skill_juben"] = "剧本",
  [":wq_skill_juben"] = "锁定技，你使用【杀】无距离限制。",
  ["$wq_skill_juben"] = "嗯？你想看我拍的电影？",
  ["#wq_skill_juben_draw"] = "摸一张牌",
  ["#wq_skill_juben_discard"] = "弃置%dest的一张牌",
  ["wq_skill_ganshe"] = "干涉",
  [":wq_skill_ganshe"] = "转换技，觉醒技，阳：准备阶段，若当前游戏轮数不小于2，你需将手牌摸至体力上限，并将技能【剧本】的描述修改为“锁定技，你使用【杀】无距离限制且限制次数+1”，然后你令此技能视为未发动过。阴：结束阶段，若你本局游戏已使用过不少于三张【杀】，你需减少1点体力上限，回复1点体力，然后为技能【剧本】增加描述“当你使用【杀】指定目标后，你可以选择一项发动：1.摸一张牌；2.弃置其一张牌”。",
  ["$wq_skill_ganshe1"] = "我会让所有人看到我，见证我。",
  ["$wq_skill_ganshe2"] = "不要去管以前发生的蠢事，想要进步的话，只管往前跑就是了。",
  ["wq_skill_wuyong"] = "无用",
  [":wq_skill_wuyong"] = "每局游戏限三次，你可以弃置一张手牌并指定一名其他角色，令其执行前【1】项：1.弃置一张牌；2.交给你一张手牌；3.受到你造成的1点伤害。然后你将令其执行的选项依次移至选项列表的末尾。每当你发动技能【干涉】后，你令本技能中括号中的数字+1（至多为3）。",
  ["$wq_skill_wuyong"] = "好戏上演！",
  ["#wq_skill_wuyong_prompt-order_1"] = "弃置一张牌",
  ["#wq_skill_wuyong_prompt-order_2"] = "交给你一张手牌",
  ["#wq_skill_wuyong_prompt-order_3"] = "受到你造成的1点伤害",
  ["#wq_skill_wuyong_prompt"] = "无用：你可以弃置一张手牌并指定一名其他角色，令其依次执行以下%arg项：%arg2",
  ["#wq_skill_wuyong_give"] = "无用：请交给%src一张手牌",
  ["~wq_dailun"] = "终究没能留下什么啊……",
}

-- 禁闭者：杜若
local wq_duruo = General:new(extension, "wq_duruo", "qun", 3, 3, General.Female)
-- 技能：祭礼
local wq_skill_jili = fk.CreateActiveSkill{
  name = "wq_skill_jili",
  anim_type = "support",
  card_num = 1,
  target_num = 1,
  prompt = function (self, selected, selected_cards)
    return "#wq_skill_jili_prompt-choose"
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isNude()
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and (not Self:prohibitDiscard(Fk:getCardById(to_select))) and Fk:getCardById(to_select).color == Card.Red
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cid = effect.cards[1]
    room:throwCard(cid, self.name, player, player)
    local target = room:getPlayerById(effect.tos[1])
    room:recover {
      who = target,
      num = 1,
      recoverBy = player,
      skillName = self.name,
    }
    if not target:isAlive() then return end
    local names = {}
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if card:isCommonTrick() and (not card.is_derived) and target:canUse(card) then
        table.insertIfNeed(names, card.name)
      end
    end
    if #names > 0 then
      U.askForUseVirtualCard(room, target, names, nil, self.name, nil, true, true, false, true)
    end
    if target:isAlive() and player:isAlive() then
      local last_id = player:getMark("wq_skill_jili_mark-last")
      if last_id and target.id == last_id then
        local choices_equip = target:getAvailableEquipSlots()
        if #choices_equip > 0 then
          local choice_equip = room:askForChoice(target, choices_equip, self.name)
          room:abortPlayerArea(target, choice_equip)
        end
      end
    end
    if player:isAlive() then
      room:setPlayerMark(player, "wq_skill_jili_mark-last", target.id)
    end
  end,
}
-- 技能：摄生
local wq_skill_shesheng = fk.CreateTriggerSkill{
  name = "wq_skill_shesheng",
  frequency = Skill.Limited,
  anim_type = "support",
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryGame) == 0 and target.dying
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local slots = table.simpleClone(target.sealedSlots)
    table.removeOne(slots, Player.JudgeSlot)
    local num = #slots
    return room:askForSkillInvoke(player, self.name, data, "#wq_skill_shesheng_prompt:"..target.id.."::"..num)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recover {
      who = target,
      num = target.maxHp - target.hp,
      recoverBy = player,
      skillName = self.name,
    }
    target:reset()
    Wqc.clearNagtiveMarks(target)
    local slots = table.simpleClone(target.sealedSlots)
    table.removeOne(slots, Player.JudgeSlot)
    local x = #slots
    if x > 0 then
      room:resumePlayerArea(target, slots)
    end
    if player:isAlive() then
      room:drawCards(player, x, self.name)
    end
  end,
}
-- 杜若的技能
wq_duruo:addSkill(wq_skill_jili)
wq_duruo:addSkill(wq_skill_shesheng)
-- 杜若的翻译
Fk:loadTranslationTable{
  ["wq_duruo"] = "杜若",
  ["#wq_duruo"] = "复生祭礼",
  ["designer:wq_duruo"] = "妙霄",
  ["cv:wq_duruo"] = "官方",
  ["illustrator:wq_duruo"] = "官方",
  ["wq_skill_jili"] = "祭礼",
  [":wq_skill_jili"] = "出牌阶段限一次，你可以弃置一张红色牌，令一名角色回复1点体力，然后其可视为使用一张普通锦囊牌。若该角色与你上一次发动本技能时的目标相同，其需废除一个装备栏。",
  ["$wq_skill_jili"] = "烟霞已升，看你们的了。",
  ["#wq_skill_jili_prompt-choose"] = "祭礼：你可以弃置一张红色牌，令一名角色回复1点体力，然后其可视为使用一张普通锦囊牌。<br>若该角色与你上一次发动本技能时的目标相同，其需废除一个装备栏。",
  ["wq_skill_shesheng"] = "摄生",
  [":wq_skill_shesheng"] = "限定技，一名角色进入濒死状态时，你可令其回复所有体力。若如此做，该角色重置其武将牌、移除所有负面标记并复原所有被废除的装备栏，然后你摸X张牌（X为其以此法复原的装备栏数量）。"
  ..Wqc.getTranslateInNewLine("#wq_common_mark_negative_description"),
  ["$wq_skill_shesheng1"] = "钟鸣天地，奏的是民之礼，颂的是人之音。",
  ["$wq_skill_shesheng2"] = "所谓“神迹”，其实早在你我之间了。",
  ["#wq_skill_shesheng_prompt"] = "摄生：你可以令%src回复所有体力、重置武将牌、移除所有负面标记、复原被废除的装备栏，然后你摸%arg张牌",
  ["~wq_duruo"] = "顺天应势吗……呵呵，真不甘心啊……",
}

return extension